/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gpiotest2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.android.gpiotest2.R;


public class GpioTest extends Activity{

    private static final String TAG = "GpioTest";

    private TextView mLog;
    private GpioPort mGpioPort;
    private ListView lv;
    String[] strArr={"gpio0","gpio1","gpio2"}; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.gpio_test);
        mLog = (TextView)findViewById(R.id.log);
        lv = (ListView) findViewById(R.id.listView1);
		
		mGpioPort =new  GpioPort();
		mGpioPort.gpio_init();
		
        if (strArr != null && strArr.length > 0) {
        	
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_list_item_1, strArr);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener (){
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
                	int data = mGpioPort.get_data(position);
                	setGpioOutput(position,data==1?0:1);        
                	 data = mGpioPort.get_data(position);
                	 mLog.setText( "get " + strArr[position] +"  "+ data);
                }
            });    	

        }      
        
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		        if (mGpioPort != null) {
				    mGpioPort.set_mode(126,1);	   
				    mGpioPort.set_dir(126,0);
				    int data = mGpioPort.get_datain(126);
				    mLog.setText( "gpio126 "  + data);
				}
			}
		});

    }
  

    
    @Override
    public void onResume() {
        super.onResume();
     
    }

    public void setGpioOutput(int gpio, int value)
    {
        if (mGpioPort != null) {
		    mGpioPort.set_mode(gpio,0);
		    mGpioPort.set_dir(gpio,1);
		    mGpioPort.set_data(gpio,value);
		}
		else
		    mLog.setText( "set " + gpio + " fail");
    }


    @Override
    public void onPause() {

        super.onPause();
    
    }

    @Override
    public void onDestroy() {
        if (mGpioPort != null) {
            mGpioPort.gpio_deinit();
            mGpioPort = null;
        }
        super.onDestroy();
    }
}


